home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / bash / bash_108 / bash-108.zoo / readline / vi_mode.c < prev   
Encoding:
C/C++ Source or Header  |  1991-08-22  |  19.2 KB  |  1,059 lines

  1. /* vi_mode.c -- A vi emulation mode for Bash.
  2.    Derived from code written by Jeff Sparkes (jeff1@????).  */
  3.  
  4. /* Copyright (C) 1988, 1991 Free Software Foundation, Inc.
  5.  
  6.    This file is part of the GNU Readline Library (the Library), a set of
  7.    routines for providing Emacs style line input to programs that ask
  8.    for it.
  9.  
  10.    The Library is free software; you can redistribute it and/or modify
  11.    it under the terms of the GNU General Public License as published by
  12.    the Free Software Foundation; either version 1, or (at your option)
  13.    any later version.
  14.  
  15.    The Library is distributed in the hope that it will be useful, but
  16.    WITHOUT ANY WARRANTY; without even the implied warranty of
  17.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18.    General Public License for more details.
  19.  
  20.    The GNU General Public License is often shipped with GNU software, and
  21.    is generally kept in a file called COPYING or LICENSE.  If you do not
  22.    have a copy of the license, write to the Free Software Foundation,
  23.    675 Mass Ave, Cambridge, MA 02139, USA. */
  24.  
  25. /* **************************************************************** */
  26. /*                                    */
  27. /*            VI Emulation Mode                */
  28. /*                                    */
  29. /* **************************************************************** */
  30.  
  31. /* Last string searched for from `/' or `?'. */
  32. static char *vi_last_search = (char *)NULL;
  33. static int vi_histpos;
  34.  
  35. /* Non-zero means enter insertion mode. */
  36. int vi_doing_insert = 0;
  37.  
  38. /* *** UNCLEAN *** */
  39. /* Command keys which do movement for xxx_to commands. */
  40. static char *vi_motion = " hl^$0ftFt;,%wbeWBE|";
  41.  
  42. /* Keymap used for vi replace characters.  Created dynamically since
  43.    rarely used. */
  44. static Keymap vi_replace_map = (Keymap)NULL;
  45.  
  46. /* The number of characters inserted in the last replace operation. */
  47. static vi_replace_count = 0;
  48.  
  49. /* Yank the nth arg from the previous line into this line at point. */
  50. rl_vi_yank_arg (count)
  51.      int count;
  52. {
  53.  
  54.   if (rl_explicit_arg)
  55.     rl_yank_nth_arg (count, 0);
  56.   else
  57.     rl_yank_nth_arg ('$', 0);
  58. }
  59.  
  60. /* With an argument, move back that many history lines, else move to the
  61.    beginning of history. */
  62. rl_vi_fetch_history (count, c)
  63.      int count, c;
  64. {
  65.   extern int rl_explicit_arg;
  66.  
  67.   if (rl_explicit_arg)
  68.     rl_get_previous_history (count);
  69.   else
  70.     rl_beginning_of_history (count, 0);
  71. }
  72.  
  73. /* Search again for the last thing searched for. */
  74. rl_vi_search_again (ignore, key)
  75.      int ignore, key;
  76. {
  77.   switch (key)
  78.     {
  79.     case 'n':
  80.       rl_vi_dosearch (vi_last_search, -1);
  81.       break;
  82.  
  83.     case 'N':
  84.       rl_vi_dosearch (vi_last_search, 1);
  85.       break;
  86.     }
  87. }
  88.  
  89. /* Do a vi style search. */
  90. rl_vi_search (count, key)
  91.      int count, key;
  92. {
  93.   int dir, c, save_pos;
  94.   char *p;
  95.  
  96.   switch (key)
  97.     {
  98.     case '?':
  99.       dir = 1;
  100.       break;
  101.  
  102.     case '/':
  103.       dir = -1;
  104.       break;
  105.  
  106.     default:
  107.       ding ();
  108.       return;
  109.     }
  110.  
  111.   vi_histpos = where_history ();
  112.   maybe_save_line ();
  113.   save_pos = rl_point;
  114.  
  115.   /* Reuse the line input buffer to read the search string. */
  116.   the_line[0] = 0;
  117.   rl_end = rl_point = 0;
  118.   p = (char *)alloca (2 + (rl_prompt ? strlen (rl_prompt) : 0));
  119.  
  120.   sprintf (p, "%s%c", rl_prompt ? rl_prompt : "", key);
  121.  
  122.   rl_message (p, 0, 0);
  123.  
  124.   while (c = rl_read_key ())
  125.     {
  126.       switch (c)
  127.     {
  128.     case CTRL('H'):
  129.     case RUBOUT:
  130.       if (rl_point == 0)
  131.         {
  132.           maybe_unsave_line ();
  133.           rl_clear_message ();
  134.           rl_point = save_pos;
  135.           return;
  136.         }
  137.  
  138.     case CTRL('W'):
  139.     case CTRL('U'):
  140.       rl_dispatch (c, keymap);
  141.       break;
  142.  
  143.     case ESC:
  144.     case RETURN:
  145.     case NEWLINE:
  146.       goto dosearch;
  147.       break;
  148.  
  149.     case CTRL('C'):
  150.       maybe_unsave_line ();
  151.       rl_clear_message ();
  152.       rl_point = 0;
  153.       ding ();
  154.       return;
  155.  
  156.     default:
  157.       rl_insert (1, c);
  158.       break;
  159.     }
  160.       rl_redisplay ();
  161.     }
  162.  dosearch:
  163.   if (vi_last_search)
  164.     free (vi_last_search);
  165.  
  166.   vi_last_search = savestring (the_line);
  167.   rl_vi_dosearch (the_line, dir);
  168. }
  169.  
  170. rl_vi_dosearch (string, dir)
  171.      char *string;
  172.      int dir;
  173. {
  174.   int old, save = vi_histpos;
  175.   HIST_ENTRY *h;
  176.  
  177.   if (string == 0 || *string == 0 || vi_histpos < 0)
  178.     {
  179.       ding ();
  180.       return;
  181.     }
  182.  
  183.   if ((save = history_search_pos (string, dir, vi_histpos + dir)) == -1)
  184.     {
  185.       maybe_unsave_line ();
  186.       rl_clear_message ();
  187.       rl_point = 0;
  188.       ding ();
  189.       return;
  190.     }
  191.  
  192.   vi_histpos = save;
  193.  
  194.   old = where_history ();
  195.   history_set_pos (vi_histpos);
  196.   h = current_history ();
  197.   history_set_pos (old);
  198.  
  199.   strcpy (the_line, h->line);
  200.   rl_undo_list = (UNDO_LIST *)h->data;
  201.   rl_end = strlen (the_line);
  202.   rl_point = 0;
  203.   rl_clear_message ();
  204. }
  205.  
  206. /* Completion, from vi's point of view. */
  207. rl_vi_complete (ignore, key)
  208.      int ignore, key;
  209. {
  210.   if ((rl_point < rl_end) && (!whitespace (the_line[rl_point])))
  211.     {
  212.       if (!whitespace (the_line[rl_point + 1]))
  213.     rl_vi_end_word (1, 'E');
  214.       rl_point++;
  215.     }
  216.  
  217.   if (key == '*')
  218.     rl_complete_internal ('*');    /* Expansion and replacement. */
  219.   else if (key == '=')
  220.     rl_complete_internal ('?');    /* List possible completions. */
  221.   else if (key == '\\')
  222.     rl_complete_internal (TAB);    /* Standard Readline completion. */
  223.   else
  224.     rl_complete (0, key);
  225.  
  226.   rl_vi_insertion_mode ();
  227. }
  228.  
  229. /* Previous word in vi mode. */
  230. rl_vi_prev_word (count, key)
  231.      int count, key;
  232. {
  233.   if (count < 0)
  234.     {
  235.       rl_vi_next_word (-count, key);
  236.       return;
  237.     }
  238.  
  239.   if (rl_point == 0)
  240.     {
  241.       ding ();
  242.       return;
  243.     }
  244.  
  245.   if (uppercase_p (key))
  246.     rl_vi_bWord (count);
  247.   else
  248.     rl_vi_bword (count);
  249. }
  250.  
  251. /* Next word in vi mode. */
  252. rl_vi_next_word (count, key)
  253.      int count;
  254. {
  255.   if (count < 0)
  256.     {
  257.       rl_vi_prev_word (-count, key);
  258.       return;
  259.     }
  260.  
  261.   if (rl_point >= (rl_end - 1))
  262.     {
  263.       ding ();
  264.       return;
  265.     }
  266.  
  267.   if (uppercase_p (key))
  268.     rl_vi_fWord (count);
  269.   else
  270.     rl_vi_fword (count);
  271. }
  272.  
  273. /* Move to the end of the ?next? word. */
  274. rl_vi_end_word (count, key)
  275.      int count, key;
  276. {
  277.   if (count < 0)
  278.     {
  279.       ding ();
  280.       return;
  281.     }
  282.  
  283.   if (uppercase_p (key))
  284.     rl_vi_eWord (count);
  285.   else
  286.     rl_vi_eword (count);
  287. }
  288.  
  289. /* Move forward a word the way that 'W' does. */
  290. rl_vi_fWord (count)
  291.      int count;
  292. {
  293.   while (count-- && rl_point < (rl_end - 1))
  294.     {
  295.       /* Skip until whitespace. */
  296.       while (!whitespace (the_line[rl_point]) && rl_point < rl_end)
  297.     rl_point++;
  298.  
  299.       /* Now skip whitespace. */
  300.       while (whitespace (the_line[rl_point]) && rl_point < rl_end)
  301.     rl_point++;
  302.     }
  303. }
  304.  
  305. rl_vi_bWord (count)
  306.      int count;
  307. {
  308.   while (count-- && rl_point > 0)
  309.     {
  310.       /* If we are at the start of a word, move back to whitespace so
  311.      we will go back to the start of the previous word. */
  312.       if (!whitespace (the_line[rl_point]) &&
  313.       whitespace (the_line[rl_point - 1]))
  314.     rl_point--;
  315.  
  316.       while (rl_point > 0 && whitespace (the_line[rl_point]))
  317.     rl_point--;
  318.  
  319.       if (rl_point > 0)
  320.     {
  321.       while (--rl_point >= 0 && !whitespace (the_line[rl_point]));
  322.       rl_point++;
  323.     }
  324.     }
  325. }
  326.  
  327. rl_vi_eWord (count)
  328.      int count;
  329. {
  330.   while (count-- && rl_point < (rl_end - 1))
  331.     {
  332.       /* Move to white space. */
  333.       while (++rl_point < rl_end && whitespace (the_line[rl_point]))
  334.     ;
  335.  
  336.       if (rl_point && rl_point < rl_end)
  337.     {
  338.       /* Skip whitespace. */
  339.       while (rl_point < rl_end && whitespace (the_line[rl_point]))
  340.         rl_point++;
  341.  
  342.       /* Skip until whitespace. */
  343.       while (rl_point < rl_end && !whitespace (the_line[rl_point]))
  344.         rl_point++;
  345.  
  346.       /* Move back to the last character of the word. */
  347.       rl_point--;
  348.     }
  349.     }
  350. }
  351.  
  352. rl_vi_fword (count)
  353.      int count;
  354. {
  355.   while (count-- && rl_point < (rl_end - 1))
  356.     {
  357.       /* Move to white space (really non-identifer). */
  358.       if (isident (the_line[rl_point]))
  359.     {
  360.       while (isident (the_line[rl_point]) && rl_point < rl_end)
  361.         rl_point++;
  362.     }
  363.       else /* if (!whitespace (the_line[rl_point])) */
  364.     {
  365.       while (!isident (the_line[rl_point]) &&
  366.          !whitespace (the_line[rl_point]) && rl_point < rl_end)
  367.         rl_point++;
  368.         }
  369.  
  370.       /* Move past whitespace. */
  371.       while (whitespace (the_line[rl_point]) && rl_point < rl_end)
  372.     rl_point++;
  373.     }
  374. }
  375.  
  376. rl_vi_bword (count)
  377.      int count;
  378. {
  379.   while (count-- && rl_point > 0)
  380.     {
  381.       int last_is_ident;
  382.  
  383.       /* If we are at the start of a word, move back to whitespace
  384.          so we will go back to the start of the previous word. */
  385.       if (!whitespace (the_line[rl_point]) &&
  386.           whitespace (the_line[rl_point - 1]))
  387.         rl_point--;
  388.  
  389.       /* If this character and the previous character are `opposite', move
  390.          back so we don't get messed up by the rl_point++ down there in
  391.          the while loop.  Without this code, words like `l;' screw up the
  392.          function. */
  393.       last_is_ident = isident (the_line[rl_point - 1]);
  394.       if ((isident (the_line[rl_point]) && !last_is_ident) ||
  395.       (!isident (the_line[rl_point]) && last_is_ident))
  396.     rl_point--;
  397.  
  398.       while (rl_point > 0 && whitespace (the_line[rl_point]))
  399.     rl_point--;
  400.  
  401.       if (rl_point > 0)
  402.     {
  403.       if (isident (the_line[rl_point]))
  404.         while (--rl_point >= 0 && isident (the_line[rl_point]));
  405.       else
  406.         while (--rl_point >= 0 && !isident (the_line[rl_point]) &&
  407.            !whitespace (the_line[rl_point]));
  408.       rl_point++;
  409.     }
  410.     }
  411. }
  412.  
  413. rl_vi_eword (count)
  414.      int count;
  415. {
  416.   while (count-- && rl_point < rl_end - 1)
  417.     {
  418.       while (++rl_point < rl_end && whitespace (the_line[rl_point]))
  419.         ;
  420.  
  421.       if (rl_point < rl_end)
  422.         {
  423.           if (isident (the_line[rl_point]))
  424.             while (++rl_point < rl_end && isident (the_line[rl_point]));
  425.           else
  426.             while (++rl_point < rl_end && !isident (the_line[rl_point])
  427.                    && !whitespace (the_line[rl_point]));
  428.           rl_point--;
  429.         }
  430.     }
  431. }
  432.  
  433. rl_vi_insert_beg ()
  434. {
  435.   rl_beg_of_line ();
  436.   rl_vi_insertion_mode ();
  437.   return 0;
  438. }
  439.  
  440. rl_vi_append_mode ()
  441. {
  442.   if (rl_point < rl_end)
  443.     rl_point += 1;
  444.   rl_vi_insertion_mode ();
  445.   return 0;
  446. }
  447.  
  448. rl_vi_append_eol ()
  449. {
  450.   rl_end_of_line ();
  451.   rl_vi_append_mode ();
  452.   return 0;
  453. }
  454.  
  455. /* What to do in the case of C-d. */
  456. rl_vi_eof_maybe (count, c)
  457.      int count, c;
  458. {
  459.   rl_newline (1, '\n');
  460. }
  461.  
  462. /* Insertion mode stuff. */
  463.  
  464. /* Switching from one mode to the other really just involves
  465.    switching keymaps. */
  466. rl_vi_insertion_mode ()
  467. {
  468.   keymap = vi_insertion_keymap;
  469. }
  470.  
  471. rl_vi_movement_mode ()
  472. {
  473.   if (rl_point > 0)
  474.     rl_backward (1);
  475.  
  476.   keymap = vi_movement_keymap;
  477.   vi_done_inserting ();
  478. }
  479.  
  480. vi_done_inserting ()
  481. {
  482.   if (vi_doing_insert)
  483.     {
  484.       rl_end_undo_group ();
  485.       vi_doing_insert = 0;
  486.     }
  487. }
  488.  
  489. rl_vi_arg_digit (count, c)
  490.      int count, c;
  491. {
  492.   if (c == '0' && rl_numeric_arg == 1 && !rl_explicit_arg)
  493.     rl_beg_of_line ();
  494.   else
  495.     rl_digit_argument (count, c);
  496. }
  497.  
  498. /* Doesn't take an arg count in vi */
  499. rl_vi_change_case (ignore1, ignore2)
  500.      int ignore1, ignore2;
  501. {
  502.   char c = 0;
  503.  
  504.   /* Don't try this on an empty line. */
  505.   if (rl_point >= rl_end)
  506.     return;
  507.  
  508.   if (uppercase_p (the_line[rl_point]))
  509.     c = to_lower (the_line[rl_point]);
  510.   else if (lowercase_p (the_line[rl_point]))
  511.     c = to_upper (the_line[rl_point]);
  512.  
  513.   /* Vi is kind of strange here. */
  514.   if (c)
  515.     {
  516.       rl_begin_undo_group ();
  517.       rl_delete (1, c);
  518.       rl_insert (1, c);
  519.       rl_end_undo_group ();
  520.       rl_vi_check ();
  521.     }
  522.   else
  523.     rl_forward (1);
  524. }
  525.  
  526. rl_vi_put (count, key)
  527.      int count, key;
  528. {
  529.   if (!uppercase_p (key) && (rl_point + 1 <= rl_end))
  530.     rl_forward (1);
  531.  
  532.   rl_yank ();
  533.   rl_backward (1);
  534. }
  535.  
  536. rl_vi_check ()
  537. {
  538.   if (rl_point && rl_point == rl_end)
  539.     rl_point--;
  540. }
  541.  
  542. rl_vi_column (count)
  543. {
  544.   if (count > rl_end)
  545.     rl_end_of_line ();
  546.   else
  547.     rl_point = count - 1;
  548. }
  549.  
  550. int
  551. rl_vi_domove (key, nextkey)
  552.      int key, *nextkey;
  553. {
  554.   int c, save;
  555.  
  556.   rl_mark = rl_point;
  557.   c = rl_read_key ();
  558.   *nextkey = c;
  559.  
  560.   if (!member (c, vi_motion))
  561.     {
  562.       if (digit (c))
  563.     {
  564.       save = rl_numeric_arg;
  565.       rl_digit_loop1 ();
  566.       rl_numeric_arg *= save;
  567.     }
  568.       else if ((key == 'd' && c == 'd') ||
  569.            (key == 'y' && c == 'y') ||
  570.            (key == 'c' && c == 'c'))
  571.     {
  572.       rl_mark = rl_end;
  573.       rl_beg_of_line ();
  574.       return (0);
  575.     }
  576.       else
  577.     return (-1);
  578.     }
  579.   else
  580.     {
  581.       /* If the command is cw or cW, we don't want whitespace after the
  582.      end fo the word eaten, so change the `w' or `W' to `e' or `E'. */
  583.       if (key == 'c' && (c == 'w' || c == 'W'))
  584.     c = (c == 'w') ? 'e' : 'E';
  585.     }
  586.  
  587.   rl_dispatch (c, keymap);
  588.  
  589.   /* No change in position means the command failed. */
  590.   if (rl_mark == rl_point)
  591.     return (-1);
  592.  
  593.   if ((c == 'w' || c == 'W') && rl_point < rl_end)
  594.     rl_point--;
  595.  
  596.   if (rl_mark < rl_point)
  597.     exchange (rl_point, rl_mark);
  598.  
  599.   return (0);
  600. }
  601.  
  602. /* A simplified loop for vi. Don't dispatch key at end.
  603.    Don't recognize minus sign? */
  604. rl_digit_loop1 ()
  605. {
  606.   int key, c;
  607.  
  608.   while (1)
  609.     {
  610.       rl_message ("(arg: %d) ", arg_sign * rl_numeric_arg, 0);
  611.       key = c = rl_read_key ();
  612.  
  613.       if (keymap[c].type == ISFUNC &&
  614.       keymap[c].function == rl_universal_argument)
  615.     {
  616.       rl_numeric_arg *= 4;
  617.       continue;
  618.     }
  619.  
  620.       c = UNMETA (c);
  621.       if (numeric (c))
  622.     {
  623.       if (rl_explicit_arg)
  624.         rl_numeric_arg = (rl_numeric_arg * 10) + (c - '0');
  625.       else
  626.         rl_numeric_arg = (c - '0');
  627.       rl_explicit_arg = 1;
  628.     }
  629.       else
  630.     {
  631.       rl_clear_message ();
  632.       rl_stuff_char (key);
  633.       break;
  634.     }
  635.     }
  636. }
  637.  
  638. rl_vi_delete_to (count, key)
  639.      int count, key;
  640. {
  641.   int c;
  642.  
  643.   if (uppercase_p (key))
  644.     rl_stuff_char ('$');
  645.  
  646.   if (rl_vi_domove (key, &c))
  647.     {
  648.       ding ();
  649.       return;
  650.     }
  651.  
  652.   if ((c != '|') && (c != 'h') && rl_mark < rl_end)
  653.     rl_mark++;
  654.  
  655.   rl_kill_text (rl_point, rl_mark);
  656. }
  657.  
  658. rl_vi_change_to (count, key)
  659.      int count, key;
  660. {
  661.   int c;
  662.  
  663.   if (uppercase_p (key))
  664.     rl_stuff_char ('$');
  665.  
  666.   if (rl_vi_domove (key, &c))
  667.     {
  668.       ding ();
  669.       return;
  670.     }
  671.  
  672.   if ((c != '|') && (c != 'h') && rl_mark < rl_end)
  673.     rl_mark++;
  674.  
  675.   rl_begin_undo_group ();
  676.   vi_doing_insert = 1;
  677.   rl_kill_text (rl_point, rl_mark);
  678.   rl_vi_insertion_mode ();
  679. }
  680.  
  681. rl_vi_yank_to (count, key)
  682.      int count, key;
  683. {
  684.   int c, save = rl_point;
  685.  
  686.   if (uppercase_p (key))
  687.     rl_stuff_char ('$');
  688.  
  689.   if (rl_vi_domove (key, &c))
  690.     {
  691.       ding ();
  692.       return;
  693.     }
  694.  
  695.   rl_begin_undo_group ();
  696.   rl_kill_text (rl_point, rl_mark);
  697.   rl_end_undo_group ();
  698.   rl_do_undo ();
  699.   rl_point = save;
  700. }
  701.  
  702. rl_vi_delete (count)
  703. {
  704.   int end;
  705.  
  706.   if (rl_end == 0)
  707.     {
  708.       ding ();
  709.       return;
  710.     }
  711.  
  712.   end = rl_point + count;
  713.  
  714.   if (end >= rl_end)
  715.     end = rl_end;
  716.  
  717.   rl_kill_text (rl_point, end);
  718.   
  719.   if (rl_point > 0 && rl_point == rl_end)
  720.     rl_backward (1);
  721. }
  722.  
  723. /* Turn the current line into a comment in shell history.
  724.    A K*rn shell style function. */
  725. rl_vi_comment ()
  726. {
  727.   rl_beg_of_line ();
  728.   rl_insert_text (": ");    /* `#' doesn't work in interactive mode */
  729.   rl_redisplay ();
  730.   rl_newline (1, '\010');
  731. }
  732.  
  733. rl_vi_first_print ()
  734. {
  735.   rl_back_to_indent ();
  736. }
  737.  
  738. rl_back_to_indent (ignore1, ignore2)
  739.      int ignore1, ignore2;
  740. {
  741.   rl_beg_of_line ();
  742.   while (rl_point < rl_end && whitespace (the_line[rl_point]))
  743.     rl_point++;
  744. }
  745.  
  746. /* NOTE: it is necessary that opposite directions are inverses */
  747. #define    FTO     1        /* forward to */
  748. #define BTO    -1        /* backward to */
  749. #define FFIND     2        /* forward find */
  750. #define BFIND    -2        /* backward find */
  751.  
  752. rl_vi_char_search (count, key)
  753.      int count, key;
  754. {
  755.   static char target;
  756.   static int orig_dir, dir;
  757.   int pos;
  758.  
  759.   if (key == ';' || key == ',')
  760.     dir = (key == ';' ? orig_dir : -orig_dir);
  761.   else
  762.     {
  763.       target = rl_getc (in_stream);
  764.  
  765.       switch (key)
  766.     {
  767.     case 't':
  768.       orig_dir = dir = FTO;
  769.       break;
  770.  
  771.     case 'T':
  772.       orig_dir = dir = BTO;
  773.       break;
  774.  
  775.     case 'f':
  776.       orig_dir = dir = FFIND;
  777.       break;
  778.  
  779.     case 'F':
  780.       orig_dir = dir = BFIND;
  781.       break;
  782.     }
  783.     }
  784.  
  785.   pos = rl_point;
  786.  
  787.   if (dir < 0)
  788.     {
  789.       if (pos == 0)
  790.     {
  791.       ding ();
  792.       return;
  793.     }
  794.  
  795.       pos--;
  796.       do
  797.     {
  798.       if (the_line[pos] == target)
  799.         {
  800.           if (dir == BTO)
  801.         rl_point = pos + 1;
  802.           else
  803.         rl_point = pos;
  804.           return;
  805.         }
  806.     }
  807.       while (pos--);
  808.  
  809.       if (pos < 0)
  810.     {
  811.       ding ();
  812.       return;
  813.     }
  814.     }
  815.   else
  816.     {            /* dir > 0 */
  817.       if (pos >= rl_end)
  818.     {
  819.       ding ();
  820.       return;
  821.     }
  822.  
  823.       pos++;
  824.       do
  825.     {
  826.       if (the_line[pos] == target)
  827.         {
  828.           if (dir == FTO)
  829.         rl_point = pos - 1;
  830.           else
  831.         rl_point = pos;
  832.           return;
  833.         }
  834.     }
  835.       while (++pos < rl_end);
  836.  
  837.       if (pos >= (rl_end - 1))
  838.     ding ();
  839.     }
  840. }
  841.  
  842. /* Match brackets */
  843. rl_vi_match ()
  844. {
  845.   int count = 1, brack, pos;
  846.  
  847.   pos = rl_point;
  848.   if ((brack = rl_vi_bracktype (the_line[rl_point])) == 0)
  849.     {
  850.       while ((brack = rl_vi_bracktype (the_line[rl_point])) == 0 &&
  851.          rl_point < rl_end - 1)
  852.     rl_forward (1);
  853.  
  854.       if (brack <= 0)
  855.     {
  856.       rl_point = pos;
  857.       ding ();
  858.       return;
  859.     }
  860.     }
  861.  
  862.   pos = rl_point;
  863.  
  864.   if (brack < 0)
  865.     {
  866.       while (count)
  867.     {
  868.       if (--pos >= 0)
  869.         {
  870.           int b = rl_vi_bracktype (the_line[pos]);
  871.           if (b == -brack)
  872.         count--;
  873.           else if (b == brack)
  874.         count++;
  875.         }
  876.       else
  877.         {
  878.           ding ();
  879.           return;
  880.         }
  881.     }
  882.     }
  883.   else
  884.     {            /* brack > 0 */
  885.       while (count)
  886.     {
  887.       if (++pos < rl_end)
  888.         {
  889.           int b = rl_vi_bracktype (the_line[pos]);
  890.           if (b == -brack)
  891.         count--;
  892.           else if (b == brack)
  893.         count++;
  894.         }
  895.       else
  896.         {
  897.           ding ();
  898.           return;
  899.         }
  900.     }
  901.     }
  902.   rl_point = pos;
  903. }
  904.  
  905. int
  906. rl_vi_bracktype (c)
  907.      int c;
  908. {
  909.   switch (c)
  910.     {
  911.     case '(': return  1;
  912.     case ')': return -1;
  913.     case '[': return  2;
  914.     case ']': return -2;
  915.     case '{': return  3;
  916.     case '}': return -3;
  917.     default:  return  0;
  918.     }
  919. }
  920.  
  921. rl_vi_change_char ()
  922. {
  923.   int c;
  924.  
  925.   c = rl_getc (in_stream);
  926.  
  927.   switch (c)
  928.     {
  929.     case '\033':
  930.     case CTRL('C'):
  931.       return;
  932.  
  933.     default:
  934.       rl_begin_undo_group ();
  935.       rl_delete (1, c);
  936.       rl_insert (1, c);
  937.       rl_end_undo_group ();
  938.       break;
  939.     }
  940. }
  941.  
  942. rl_vi_subst (count, key)
  943.      int count, key;
  944. {
  945.   rl_begin_undo_group ();
  946.   vi_doing_insert = 1;
  947.  
  948.   if (uppercase_p (key))
  949.     {
  950.       rl_beg_of_line ();
  951.       rl_kill_line (1);
  952.     }
  953.   else
  954.     rl_delete (count, key);
  955.  
  956.   rl_vi_insertion_mode ();
  957. }
  958.  
  959. rl_vi_overstrike (count, key)
  960.      int count, key;
  961. {
  962.   int i;
  963.  
  964.   if (vi_doing_insert == 0)
  965.     {
  966.       vi_doing_insert = 1;
  967.       rl_begin_undo_group ();
  968.     }
  969.  
  970.   for (i = 0; i < count; i++)
  971.     {
  972.       vi_replace_count++;
  973.       rl_begin_undo_group ();
  974.  
  975.       if (rl_point < rl_end)
  976.     {
  977.       rl_delete (1, key);
  978.       rl_insert (1, key);
  979.     }
  980.       else
  981.     rl_insert (1, key);
  982.  
  983.       rl_end_undo_group ();
  984.     }
  985. }
  986.  
  987. rl_vi_overstrike_delete (count)
  988.      int count;
  989. {
  990.   int i, s;
  991.  
  992.   for (i = 0; i < count; i++)
  993.     {
  994.       if (vi_replace_count == 0)
  995.     {
  996.       ding ();
  997.       break;
  998.     }
  999.       s = rl_point;
  1000.  
  1001.       if (rl_do_undo ())
  1002.     vi_replace_count--;
  1003.  
  1004.       if (rl_point == s)
  1005.     rl_backward (1);
  1006.     }
  1007.  
  1008.   if (vi_replace_count == 0 && vi_doing_insert)
  1009.     {
  1010.       rl_end_undo_group ();
  1011.       rl_do_undo ();
  1012.       vi_doing_insert = 0;
  1013.     }
  1014. }
  1015.  
  1016. rl_vi_replace ()
  1017. {
  1018.   int i;
  1019.  
  1020.   vi_replace_count = 0;
  1021.  
  1022.   vi_replace_map = rl_make_bare_keymap ();
  1023.  
  1024.   for (i = ' '; i < 127; i++)
  1025.     vi_replace_map[i].function = rl_vi_overstrike;
  1026.  
  1027.   vi_replace_map[RUBOUT].function = rl_vi_overstrike_delete;
  1028.   vi_replace_map[ESC].function = rl_vi_movement_mode;
  1029.   vi_replace_map[RETURN].function = rl_newline;
  1030.   vi_replace_map[NEWLINE].function = rl_newline;
  1031.   keymap = vi_replace_map;
  1032. }
  1033.  
  1034. /*
  1035.  * Try to complete the word we are standing on or the word that ends with
  1036.  * the previous character. A space matches everything.
  1037.  * Word delimiters are space and ;.
  1038.  */
  1039. rl_vi_possible_completions()
  1040. {
  1041.   int save_pos = rl_point;
  1042.  
  1043.   if (!index (" ;", the_line[rl_point]))
  1044.     {
  1045.       while (!index(" ;", the_line[++rl_point]))
  1046.     ;
  1047.     }
  1048.   else if (the_line[rl_point-1] == ';')
  1049.     {
  1050.       ding ();
  1051.       return (0);
  1052.     }
  1053.  
  1054.   rl_possible_completions ();
  1055.   rl_point = save_pos;
  1056.  
  1057.   return (0);
  1058. }
  1059.